@extends('admin.layout.app')

@section('title', '用户列表')

@section('content')
    <div class="layui-col-lg12">
        <div class="layui-card">
            <div class="layui-card-header">
                用户列表
            </div>
            <div class="layui-card-body">
                <form class="layui-form" id="search-form" action="" onsubmit="return false;">
                    <div class="toolbar layui-form-item">
                        <div class="layui-inline">
                            <input class="layui-input" name="keyword" id="keyword" autocomplete="off" placeholder="輸入搜索的關鍵字">
                        </div>

                        <div class="layui-inline">
                            <div class="layui-btn-group">
                                <button class="layui-btn" data-type="reload" title="搜索">
                                    <i class="layui-icon layui-icon-search"></i>
                                </button>

                                <a  class="layui-btn" href="@broute('admin.create')" title="添加">
                                    <i class="layui-icon layui-icon-add-1"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </form>

                <table class="layui-table" lay-data="{url:'@broute('admin.index')', page:true, id:'users',limit:15}" lay-filter="tb">
                    <thead>
                    <tr>
                        <th lay-data="{type:'checkbox'}"></th>
                        <th lay-data="{field:'id', width:80, sort: true}">ID</th>
                        <th lay-data="{field:'staff_id', width:100}">工號</th>
                        <th lay-data="{field:'name', width:100}">姓名</th>
                        <th lay-data="{field:'nickname', width:100}">暱稱</th>
                        <th lay-data="{field:'role_names'}">角色</th>
                        <th lay-data="{field:'enable_name', width:100}">狀態</th>
                        <th lay-data="{field:'created_at', width:200}">註冊時間</th>
                        <th lay-data="{fixed: 'right', width:200, align:'center', toolbar: '#toolbar'}">操作</th>
                    </tr>
                    </thead>
                </table>
                <div class="layui-btn-group toolbar">
                    <a class="layui-btn layui-btn-danger" data-type="delete">
                        <i class="layui-icon layui-icon-delete"></i> 批量刪除
                    </a>
                </div>
            </div>
        </div>
    </div>
    <script type="text/html" id="toolbar">
        <div class="layui-btn-group">
        <a href="@broute('admin.edit', '@{{d.id}}')" class="layui-btn layui-btn-xs" lay-event="edit" title="編輯">
            <i class="layui-icon layui-icon-edit"></i>
        </a>
        <a href="javascript:;" data-url="@broute('admin.password.reset', '@{{d.id}}')" data-confirm="確認要重置成【123456】嗎？" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="reset" title="重置密碼">
            <i class="layui-icon layui-icon-password"></i>
        </a>
        </div>
    </script>
@endsection

@section('script')
<script>
$(function () {
    layui.use('table', function () {
        var table = layui.table;

        $(document).on('click', 'a[data-confirm]', function () {
            var $this = $(this);
            console.log($this);
            if (layer.confirm($this.attr('data-confirm'), function (index) {
                window.location.href = $this.attr('data-url')
            }));
        });

        var active = {
            reload: function() {
                table.reload('users', {
                    page: { curr: 1 }
                    , where: $('#search-form').serializeJSON()
                });
            },
            delete: function () { //獲取選中數據
                var checkStatus = table.checkStatus('users')
                    , data = checkStatus.data;
                // 抽取所有id
                var id = [];
                $.map(data, function (k, v) {
                    id.push(k.id)
                });
                $.ajax({
                    url: '@broute('admin.delete')',
                    type: 'post',
                    data: {'id': id},
                    success: function (data) {
                        if (!data.status) {
                            layer.msg(data.message||'error', function(){});
                            return;
                        }
                        layer.msg(data.message);
                        active['reload'].call(this);
                    }
                });
            }
        };

        $('.toolbar .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
});
</script>
@endsection


